import {
    reqGoodsInfo,
    reqAddOrUpdateShopCart
} from "@/api"
// 封装一个游客模块 生成一个随机的身份
import {
    getUUID
} from '@/utils/uuid_token'

const state = {
    goodInfo: {},
    uuid_token: getUUID()
}
const actions = {
    async getGoodInfo({
        commit
    }, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code == 200) {
            commit('GETGOODINFO', result.data)
        }
    },
    // async函数返回的是一个promise [要么成功要么失败  非空字符串代表成功]
    async addOrUpdateShopCart({
        commit
    }, {
        skuId,
        skuNum
    }) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum)
        // if (result.code == 200) {
        //     commit('ADDORUPDATESHOPCART', result.data)
        // }
        if (result.code === 200) {
            // console.log("购物车数据在服务器存储成功", result);
            return 'OK'
        } else {
            return Promise.reject(new Error('false'))
        }
    }
}
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo
    }
}
// 简化数据
const getters = {
    categoryView(state) {
        // 当服务器返回一个空对象时 需要给||一个空对象或者数组 否者控制台会出现警告
        return state.goodInfo.categoryView || []
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || []
    }
}
export default {
    state,
    actions,
    mutations,
    getters
}